//
//  LzwBellDetailController.m
//  NewHalfsugar
//
//  Created by 刘志武 on 16/1/3.
//  Copyright © 2016年 刘志武. All rights reserved.
//

#import "LzwBellDetailController.h"
#import "UMSocial.h"
#import "LzwSingleton.h"

@interface LzwBellDetailController ()<UIWebViewDelegate>

@property (nonatomic, retain) UIWebView *myAddWebView;
@property (nonatomic, retain) UIActivityIndicatorView *activityIndicator;


@end

@implementation LzwBellDetailController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor yellowColor];
    
    self.navigationItem.title = self.modleB.title;
    
    
    UIImage * LoginRightBar = [UIImage imageNamed:@"return.png"];
    LoginRightBar = [LoginRightBar imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithImage:LoginRightBar style:UIBarButtonItemStylePlain target:self action:@selector(LeftButtonAction:)];
    

    //设置收藏按钮
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithImage:[[UIImage imageNamed:@"66.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] style:UIBarButtonItemStylePlain target:self action:@selector(rightButtonAction:)];
    
    
    self.myAddWebView = [[UIWebView alloc]initWithFrame:CGRectMake(0, 0, 375 * WIDTH, 667 * WIDTH)];
    
    [self.myAddWebView setUserInteractionEnabled:YES];
    [self.myAddWebView setBackgroundColor:[UIColor clearColor]];
    [self.myAddWebView setOpaque:NO];//使网页透明
    
    [self.view addSubview:self.myAddWebView];
    
    NSString *str = @"http://m.ibantang.com/post/subject/";
    
    NSString *string = self.modleB.extend;
    
    NSString *string1 = [str stringByAppendingString:string];
    
    NSURLRequest *request =[NSURLRequest requestWithURL:[NSURL URLWithString:string1]];
    [self.view addSubview: self.myAddWebView];
    [self.myAddWebView loadRequest:request];
    
    [self.myAddWebView setDelegate:self];
    
    
    UIView *view = [[UIView alloc] initWithFrame:[[UIScreen mainScreen]bounds]];
    [view setTag:103];
    [view setBackgroundColor:[UIColor lightGrayColor]];
    [view setAlpha:0.8];
    [self.view addSubview:view];
    
    self.activityIndicator = [[UIActivityIndicatorView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 32.0f, 32.0f)];
    [self.activityIndicator setCenter:view.center];
    [self.activityIndicator setActivityIndicatorViewStyle:UIActivityIndicatorViewStyleWhite];
    [view addSubview:self.activityIndicator];
    [self.view addSubview:self.myAddWebView];
    [view release];
    [_myAddWebView release];
    
}

-(void)webViewDidStartLoad:(UIWebView *)webView{
    
    
    [self.activityIndicator startAnimating];
    
    
    
}

-(void)webViewDidFinishLoad:(UIWebView *)webView{
    
    [self.activityIndicator stopAnimating];
    UIView *view = (UIView *)[self.view viewWithTag:103];
    [view removeFromSuperview];
    
}

-(void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error{
    
//    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"亲 网络不给力" preferredStyle:UIAlertControllerStyleAlert];
//    
//    
//    [self presentViewController:alert animated:YES completion:^{
//        
//        
//    }];
//    UIAlertAction *ale = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
//        
//        [self.navigationController popToRootViewControllerAnimated:YES];
//        
//    }];
//    
//    [alert addAction:ale];
    
    
}

//收藏按钮触发方法
- (void)rightButtonAction:(UIBarButtonItem *)but{
    
    
    //逻辑:判断数据库是否已经收藏,如果收藏过就提示该活动应经收藏,没有就插入
    
    
    
    NSMutableArray *array = [[LzwSingleton shareDataBase] selectBellTable];
    
    
 //   判断是否已经收藏,得先把数据库里面的值取出来,遍历放入Modle对象里,在对象里面把content取出来,在放入数组中,此时这个数组放的全是content,然后在判断这个数组是否包含一样的content;
    
    NSMutableArray *contentArray = [NSMutableArray array];
    
    for (LzwBellModle *modles in array) {
        
        [contentArray addObject:modles.title];
        
    }
    
    if ([contentArray containsObject:self.modleB.title]) {
        
        
        
        UIAlertController *aler = [UIAlertController alertControllerWithTitle:@"提示" message:@"该物品已经被收藏!!!" preferredStyle:UIAlertControllerStyleAlert];
        
        [self presentViewController:aler animated:YES completion:^{
            [self dismissViewControllerAnimated:YES completion:^{
                
                
            }];
            
        }];
        return;
    }else{
        
        [[LzwSingleton shareDataBase] insertLzwBellTable:self.modleB];
        
        UIAlertController *con = [UIAlertController alertControllerWithTitle:@"提示" message:@"收藏成功" preferredStyle:UIAlertControllerStyleAlert];
        [self presentViewController:con animated:YES completion:^{
            
            [self dismissViewControllerAnimated:YES completion:^{
                
                
            }];
            
        }];
        
    }

    
    
}

//左面按钮返回状态
- (void)LeftButtonAction:(UIBarButtonItem *)bar{
    [self.navigationController popToRootViewControllerAnimated:YES];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
